﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace LevelEditor
{
    public partial class MainLevelEditorForm : Form
    {
        private bool terrainEditorActivated = false;
        public MainLevelEditorForm()
        {
            InitializeComponent();
        }

        private void MainLevelEditorForm_Load(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Maximized;
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void helpToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("I bet you would like some help!");
        }

        private void MainLevelEditorForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            var result = MessageBox.Show("Are you sure you would like to close the Level Editor?", "Level Editor Closing", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            if (result == DialogResult.No)
            {
                e.Cancel = true;
            }
        }

        private void terrainEditorToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (terrainEditorActivated == false)
            {
                terrainEditorActivated = true;
                terrainEditorButton.Image = Properties.Resources.TerrainButtonClicked;
            }
            else
            {
                terrainEditorActivated = false;
                terrainEditorButton.Image = Properties.Resources.TerrainButton;
            }
        }

        private void terrainEditorButton_Click(object sender, EventArgs e)
        {
            if (terrainEditorActivated == false)
            {
                terrainEditorActivated = true;
                terrainEditorButton.Image = Properties.Resources.TerrainButtonClicked;
            } 
            else
            {
                terrainEditorActivated = false;
                terrainEditorButton.Image = Properties.Resources.TerrainButton;
            }   
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Stream myStream = null;
            OpenFileDialog fileDialog = new OpenFileDialog();

            fileDialog.Title = "Open Existing Map";
            fileDialog.InitialDirectory = "c:\\";
            fileDialog.Filter = "xml files (*.xml)|*.xml|All files (*.*)|*.*";
            fileDialog.FilterIndex = 2;
            fileDialog.RestoreDirectory = true;

            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    if ((myStream = fileDialog.OpenFile()) != null)
                    {
                        using (myStream)
                        {
                            // Insert code to read the stream here.
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
                }
            }
        }

        private void MainLevelEditorForm_MouseClick(object sender, MouseEventArgs e)
        {
            if (terrainEditorActivated == true)
            {
                MessageBox.Show("You have clicked here:" + MousePosition.X + ", " + (MousePosition.Y - toolStrip1.Height - menuStrip1.Height - SystemInformation.CaptionHeight));
            }
        }

        private void MainLevelEditorForm_MouseMove(object sender, MouseEventArgs e)
        {
            labelMousePosition.Text = MousePosition.X + ", " + (MousePosition.Y - menuStrip1.Height - toolStrip1.Height - SystemInformation.CaptionHeight);
        }

        private void MainLevelEditorForm_MouseLeave(object sender, EventArgs e)
        {
            labelMousePosition.Text = "";
        }

        private void backgroundImageButton_Click(object sender, EventArgs e)
        {
            Stream myStream = null;
            OpenFileDialog fileDialog = new OpenFileDialog();

            fileDialog.Title = "Bakcground Image";
            fileDialog.InitialDirectory = "c:\\Users" + Environment.UserName + "\\My Documents\\";
            fileDialog.Filter = "Image Files(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|All files (*.*)|*.*";
            fileDialog.FilterIndex = 2;
            fileDialog.RestoreDirectory = true;

            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    if ((myStream = fileDialog.OpenFile()) != null)
                    {
                        using (myStream)
                        {
                            // Insert code to read the stream here.
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
                }
            }

        }
    }
}
